﻿//*************************************************************************************************
// XmlMessageEditor.cs
// Owner: Mahipal Kante
//
// Web Test Plugin for editing XML string type Web Service Requests
//
// Copyright(c) Microsoft Corporation, 2010
//*************************************************************************************************
using Microsoft.VisualStudio.TestTools.WebTesting;
using WebTest.WebService.Plugin.ResultTab;
using WebTest.WebService.Plugin.Runtime;

namespace WebTest.WebService.Plugin.MessageEditors
{
    /// <summary>
    /// Editor for generic text based hierarchical messages such as XML, JSON etc
    /// </summary>
    public class XmlMessageEditor : IStringHttpBodyEditorPlugin
    {
        public XmlMessageEditor()
        {
        }

        /// <summary>
        /// Determine if this plugin supports a content type.
        /// </summary>
        /// <param name="contentType">The content type to test.</param>
        /// <returns>Returns true if the plugin supports the specified content type.</returns>
        public bool SupportsContentType(string contentType)
        {
            return (ContentHandlerFactory.GetHandler(contentType) != null);
        }

        /// <summary>
        /// Create a UserControl to edit the specified bytes.  This control will be hosted in the
        /// plugin dialog which provides OK and Cancel buttons.
        /// </summary>
        /// <param name="contentType">The content type of the BinaryHttpBody.</param>
        /// <param name="initialValue">
        /// The bytes to edit.  The bytes are the payload of a BinaryHttpBody.
        /// </param>
        /// <returns>A UserControl capable of displaying and editing the byte array 
        /// value of the specified content type.
        /// </returns>
        public object CreateEditor(string contentType, string initialValue)
        {
            contentHandler = ContentHandlerFactory.GetHandler(contentType);
            contentHandler.MessageString = initialValue;
            messageEditor = new MessageEditor(contentHandler);
            return messageEditor;
        }

        /// <summary>
        /// Gets the edited bytes after the OK button is clicked on the plugin dialog.
        /// </summary>
        public string GetNewValue()
        {
            messageEditor.UpdateContent();
            return contentHandler.MessageString;
        }

        private MessageEditor messageEditor;
        private ContentHandler contentHandler = null;
    }
}
